package ca.wilkinsonlab.cardioshare;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ca.wilkinsonlab.sadi.tasks.QueryTask;

public class DummyQueryTask extends QueryTask
{
	private final static Log log = LogFactory.getLog(DummyQueryTask.class);

	private static Map<String, String> makeMap(String... values)
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put( "id", values[0] );
		map.put( "sex", values[1] );
		map.put( "age", values[2] );
		return map;
	}
	
	private List<Map<String, String>> getSimpleResults()
	{
		List<Map<String, String>> results = new ArrayList<Map<String, String>>();
		results.add( makeMap( "113356", "male", "45" ) );
		results.add( makeMap( "177738", "male", "59" ) );
		results.add( makeMap( "224300", "female", "64" ) );
		results.add( makeMap( "224404", "male", "53" ) );
		results.add( makeMap( "341328", "male", "71" ) );
		results.add( makeMap( "393177", "female", "59" ) );
		results.add( makeMap( "539529", "male", "60" ) );
		results.add( makeMap( "549915", "male", "65" ) );
		results.add( makeMap( "676732", "female", "62" ) );
		results.add( makeMap( "718984", "female", "74" ) );
		results.add( makeMap( "829059", "male", "54" ) );
		results.add( makeMap( "858974", "male", "66" ) );
		return results;
	}
	
	private List<Map<String, String>> getBODEResults()
	{
		List<Map<String, String>> results = new ArrayList<Map<String, String>>();
		results.add( makeMap( "224300", "female", "64" ) );
		results.add( makeMap( "873629", "male", "62" ) );
		results.add( makeMap( "883943", "male", "57" ) );
		results.add( makeMap( "676732", "female", "62" ) );
		results.add( makeMap( "539529", "male", "60" ) );
		results.add( makeMap( "549915", "male", "65" ) );
		results.add( makeMap( "207903", "male", "66" ) );
		return results;
	}
	
	public DummyQueryTask(String queryString)
	{
		super(queryString);
	}
	
	public void run()
	{
        if ( queryString.matches("(?s).*COPDPatientSimple.*") )
        	results.addAll( getSimpleResults() );
        else
        	results.addAll( getBODEResults() );
        try {
            log.trace("retrieving triples from http://bioinfo.icapture.ubc.ca/FakeCOPDEnumerator");
        	Thread.sleep(1000 * (int)(5 + Math.random() * 5));
        } catch (InterruptedException e ) {
        	log.warn(e);
        }
        try {
            log.trace("retrieving triples from http://bioinfo.icapture.ubc.ca/FakePatientData");
        	Thread.sleep(1000 * (int)(5 + Math.random() * 5));
        } catch (InterruptedException e ) {
        	log.warn(e);
        }
        success();
	}
}
